package Java道题.狼羊白菜问题;

import java.util.Arrays;

/**
 * @description:
 * @author: ${ywk}
 * @date: 2020-05-17
 */
public class 狼羊白菜问题 {
    public static void main(String[] args) {
        int arr[] = {0, 0, 0, 0};
        solution(arr, Integer.MAX_VALUE,0);

    }

    public static void solution(int[] arr, int index,int deep) {
        if (isEnd(arr)) {
            System.out.println(Arrays.toString(arr));
        }
        for (int i = deep; i < arr.length - 1; i++) {
            if (index == i) {
                continue;
            }
            if (arr[arr.length - 1] == 1) {

                arr[arr.length - 1] = 0;
                for (int j = 0; j < 2; j++) {
                    if (j == 0) {
                        arr[i] = 0;
                        if (!isValid(arr)) {
                            index = i;
                            arr[i] = 1;
                            continue;

                        }
                    } else {
                        arr[i] = 1;
                        if (!isValid(arr)) {
                            arr[i] = 1;
                            continue;

                        }

                    }

                    System.out.println(Arrays.toString(arr));
                    solution(arr, index,deep);
                }
            } else {
                if (arr[i] == 1) {
                    continue;
                }
                arr[arr.length - 1] = 1;
                arr[i] = 1;
                if (!isValid(arr)) {
                    arr[arr.length - 1] = 0;
                    arr[i] = 0;
                    continue;
                }
                index = i;
                System.out.println(Arrays.toString(arr));
                solution(arr, index,deep);
            }
        }
    }

    public static boolean isEnd(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 1) {
                return false;
            }
        }
        return true;
    }

    public static boolean isSameWay(int[] arr, int[] newArr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != newArr[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean isValid(int[] arr) {
        int com = 0;
        if (arr[arr.length - 1] == 0) {
            com = 1;
        }
        for (int i = 0; i < arr.length - 2; i++) {
            if (arr[i] == arr[i + 1] && arr[i] == com) {
                return false;
            }

        }
        return true;
    }
}
